<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\User;
use app\index\validate\User as UserVlidate;
use think\Db;

class login extends Common
{
    static $Cmodel;
    public function _initialize(){
        parent::_initialize();
        $this->Cmodel = new User();
    }
    
    /**
     * 登录
     */
    public function index(){
        if (!empty(session('user'))) {
          $this->redirect('/');
        }
        return $this->fetch();
    }
    
    /**
     * 登录
     */
    public function dologin(){
      if ($this->request->isPost()) {
          $data = $this->request->param();
          $user = Db('user')->where('username',$data['username'])->find();
          if (md5($data['password'])!=$user['password']) {
              return json(['status'=>-1,'info'=>'用户名密码不正确']);
          }
          $userinfo = Db('user_info')->where('uid',$user['id'])->find();
          $user['avatar'] = $userinfo['avatar'];
          $user['qq'] = $userinfo['qq'];
          $user['info'] = $userinfo['info'];
          session('user',$user);
          return json(['status'=>1,'info'=>'登录成功']);
      }
    }


    /**
     * 
     */
    
    public function loginout()
    {
      session('user',null);
      $this->redirect('/login');
    }
    /**
     * 注册
     */
    // public function register(){
    //     return $this->fetch();
    // }
    
    /**
     * 注册
     */
    // public function doregister(){
    //     $data = $this->request->param();
    //     $data['user_type'] = 1;
    //     $data['create_time'] = $data['update_time'] = time();
    //     $data['status'] = 1;
    //     // $data['user_type'] = 1;
    //     // $data['user_type'] = 1;
    //     $validate = new UserVlidate();
    //     if(!$validate->check($data)){
    //         return $validate->getError();
    //     }
    //     $result = $this->Cmodel->allowField(true)->save($data);
    //     if ($result) {
    //        return join(['status'=>1,'info'=>'注册成功']);
    //     }
        
    // }
    // 
    /**
     * 
     */
    public function loginouts(){
      diring();
    }

}
